home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 …ember: Reference Library / Dev.CD Dec 94.toast / What's New? / Sample Code / Snippets Update / ColorizePict ƒ / Colorize.c < prev    next >
Encoding:
Text File  |  1994-10-21  |  7.2 KB  |  311 lines  |  [TEXT/KAHL]

  1. // routine to colorize a pixmap.  This puts up a dialog with two colored boxes,
  2. // click in the box to select a foreground a background image to used to colorize
  3. // the picture.
  4. //
  5. // The image colorization is done using copybits, the color selection is done using
  6. // the standard color picker.
  7. //
  8. // Nick Thompson, June '94
  9.  
  10.  
  11. #include <QDOffscreen.h>
  12. #include <PictUtil.h>
  13. #include <picker.h>
  14.  
  15.  
  16.  
  17.  
  18. //------------------------
  19. // items in the color selection dialog
  20.  
  21. enum {
  22.     kColorSelectionDialogOKBtn = 1,
  23.     kColorSelectionDialogCancelBtn,
  24.     kColorSelectionDialogInfoIcn,
  25.     kColorSelectionDialogStaticText1,
  26.     kColorSelectionDialogFGColorBox,
  27.     kColorSelectionDialogBGColorBox
  28.     
  29.     // we don't care about the rest at this point
  30. } ;
  31.  
  32. static const RGBColor    kRGBBlack = {0, 0, 0};
  33. static const RGBColor    kRGBWhite = {0xFFFF, 0xFFFF, 0xFFFF};
  34.  
  35. static    RGBColor    theFGColor ;
  36. static    RGBColor    theBGColor ;
  37.  
  38. //-----------------------------------------------------------------------------------------
  39.  
  40. void draw_image(    CGrafPtr windowToColorize, 
  41.                     short x, 
  42.                     short y,
  43.                     RGBColor *fg, 
  44.                     RGBColor *bg )
  45. {
  46.     CGrafPtr        savedPort ;
  47.     GDHandle        oldDevice ;
  48.     GWorldPtr        myNewWorld ;
  49.     PixMapHandle    myOffPixMap ;
  50.     
  51.     GetGWorld( &savedPort, &oldDevice ) ;
  52.     
  53.     // get the GWorld from the window refcon
  54.     if((myNewWorld = (GWorldPtr)GetWRefCon ( (GrafPtr)windowToColorize )) == nil)
  55.         return ;
  56.         
  57.     if((myOffPixMap = GetGWorldPixMap( myNewWorld )) == nil )
  58.         return ;
  59.     
  60.     // set up the foreground and background colors so 
  61.     // that we have everything as expecte.  These are 
  62.     // the values passed in.
  63.     
  64.     SetGWorld( myNewWorld, nil ) ;
  65.     RGBForeColor( fg ) ;
  66.     RGBBackColor( bg ) ;
  67.  
  68.     (void) LockPixels( myOffPixMap ) ;
  69.     
  70.     CopyBits( &((GrafPtr)myNewWorld)->portBits,
  71.               &((GrafPtr)myNewWorld)->portBits,
  72.               &((GrafPtr)myNewWorld)->portRect,
  73.               &((GrafPtr)myNewWorld)->portRect,
  74.               srcCopy,
  75.               nil ) ;
  76.               
  77.     (void) UnlockPixels( myOffPixMap ) ;
  78.     
  79.     RGBForeColor( &kRGBBlack ) ;        // ensure the fg and bg colors are 
  80.     RGBBackColor( &kRGBWhite ) ;        // as anticipated
  81.     
  82.     SetGWorld( windowToColorize, nil ) ;
  83.     
  84.     InvalRect( &windowToColorize->portRect ) ;
  85.  
  86.     SetGWorld( savedPort, oldDevice ) ;
  87. }
  88.  
  89.  
  90. pascal Boolean OurFilter(DialogPtr theColorSelectionDialog, EventRecord *event, short *itemHit)
  91. {
  92.  
  93.     ModalFilterUPP         theProc ;
  94.     Boolean                retVal ;
  95.     
  96.     static    Boolean        isDisabled = false ;
  97.     OSErr                theErr = noErr ;
  98.     
  99.     // stuff for getditems etc
  100.     Str255                theItem ;
  101.     short                iKind;
  102.     Handle                iHandle;
  103.     Rect                iRect;
  104.     
  105.     
  106.     // get the std filter proc, again this is system 7 specific
  107.     // we are usingthe standard filter proc to render the default button.
  108.     // in a real app we'd at least need to handle update events in the filter
  109.     // proc, in order to redraw the user item, however I'm not doing
  110.     // that here because I am very lazy.  You should if you use this approach.
  111.     
  112.     theErr = GetStdFilterProc( &theProc ) ;
  113.     
  114.     if( theErr != noErr )
  115.         ExitToShell() ;
  116.         
  117.     // try to call the standard filter, if it handles the event, we don't
  118.     if( !(retVal = CallModalFilterProc(theProc, theColorSelectionDialog, event, itemHit)) )
  119.     {
  120.         switch (event->what) {
  121.     
  122.             case nullEvent:
  123.                 break;
  124.     
  125.             case keyDown:
  126.             case autoKey:
  127.                 retVal = false;
  128.                 break ;
  129.     
  130.             case updateEvt:
  131.                 // set up the foreground color
  132.                 GetDItem ( theColorSelectionDialog, kColorSelectionDialogFGColorBox, &iKind, &iHandle, &iRect) ;
  133.             
  134.                 RGBForeColor ( &theFGColor );
  135.                 FillRect( &iRect, &black) ;
  136.                 
  137.                 RGBForeColor ( &kRGBBlack );
  138.                 PenSize( 1,1) ;
  139.                 InsetRect( &iRect, -4, -4 ) ;
  140.                 FrameRect( &iRect ) ;
  141.                 PenNormal() ;
  142.  
  143.                 // set up the background color
  144.                 GetDItem ( theColorSelectionDialog, kColorSelectionDialogBGColorBox, &iKind, &iHandle, &iRect) ;
  145.  
  146.                 RGBForeColor ( &theBGColor );
  147.                 FillRect( &iRect , &black) ;
  148.                 
  149.                 RGBForeColor ( &kRGBBlack );
  150.                 PenSize( 1,1) ;
  151.                 InsetRect( &iRect, -4, -4 ) ;
  152.                 FrameRect( &iRect ) ;
  153.                 PenNormal() ;
  154.                 
  155.                 retVal = false;
  156.                 break ;
  157.     
  158.             default:
  159.                 retVal = false;
  160.                 break ;
  161.         }
  162.     }
  163.     
  164.     return retVal ;
  165. }
  166.  
  167.  
  168.  
  169.  
  170. ColorizeImage( WindowPtr imageToColorize )
  171. {
  172.  
  173.     DialogPtr        theColorSelectionDialog ;
  174.     GrafPtr            savedPort ;
  175.     
  176.     short            iKind;
  177.     Handle            iHandle;
  178.     Rect            iRect;
  179.     
  180.     RGBColor        savedFGColor ;
  181.     RGBColor        savedBGColor ;
  182.     
  183.     OSErr            theErr ;
  184.     short            itemHit ;   
  185.     
  186.     Point            where = { 0, 0 } ;
  187.     
  188.     GetPort(&savedPort ) ;
  189.     
  190.     
  191.     // post the all purpose color dialog!! use this to get the fg and bg colors
  192.     // as required by the user
  193.     
  194.     // get the dialog
  195.     theColorSelectionDialog = GetNewDialog ( 129, nil, (WindowPtr)-1 );
  196.  
  197.     SetPort( (GrafPtr)theColorSelectionDialog ) ;
  198.     
  199.     // need to ensure the fg and bg colors are filled with approprite colors.
  200.     // just use the defaults.
  201.      
  202.     GetForeColor( &theFGColor ) ;
  203.     savedFGColor = theFGColor ;
  204.     
  205.     GetBackColor( &theBGColor ) ;
  206.     savedBGColor = theBGColor ;
  207.     
  208.     GetDItem ( theColorSelectionDialog, kColorSelectionDialogFGColorBox, &iKind, &iHandle, &iRect) ;
  209.  
  210.     RGBForeColor ( &theFGColor );
  211.     FillRect( &iRect, &black) ;
  212.     
  213.     RGBForeColor ( &kRGBBlack );
  214.     PenSize( 1,1) ;
  215.     InsetRect( &iRect, -4, -4 ) ;
  216.     FrameRect( &iRect ) ;
  217.     PenNormal() ;
  218.     
  219.     GetDItem ( theColorSelectionDialog, kColorSelectionDialogBGColorBox, &iKind, &iHandle, &iRect) ;
  220.  
  221.     RGBForeColor ( &theBGColor );
  222.     FillRect( &iRect , &black) ;
  223.     
  224.     RGBForeColor ( &kRGBBlack );
  225.     PenSize( 1,1) ;
  226.     InsetRect( &iRect, -4, -4 ) ;
  227.     FrameRect( &iRect ) ;
  228.     PenNormal() ;
  229.     
  230.     
  231.     // this is the system 7 way of handling the dialog default item,
  232.     // and the cancel button for the dialog.
  233.     //
  234.     // make sure we are on sys 7 before we get here, or highlight the 
  235.     // default item in the regular way and handle key presses for ok and
  236.     // cancel in the filterproc.
  237.     
  238.     if((theErr =  SetDialogDefaultItem(theColorSelectionDialog, kColorSelectionDialogOKBtn)) != noErr )
  239.         ExitToShell() ;
  240.  
  241.     if((theErr =  SetDialogCancelItem(theColorSelectionDialog, kColorSelectionDialogCancelBtn)) != noErr )
  242.         ExitToShell() ;
  243.  
  244.     
  245.     do {
  246.     
  247.         ModalDialog ( OurFilter, &itemHit );
  248.         
  249.         switch( itemHit ) {
  250.             
  251.             case kColorSelectionDialogFGColorBox:
  252.             
  253.                 if( GetColor( where, "\pEnter the Background color:", &theFGColor, &theFGColor) ) {
  254.                     // set up the foreground color
  255.                     GetDItem ( theColorSelectionDialog, kColorSelectionDialogFGColorBox, &iKind, &iHandle, &iRect) ;
  256.                 
  257.                     RGBForeColor ( &theFGColor );
  258.                     FillRect( &iRect, &black) ;
  259.                     
  260.                     RGBForeColor ( &kRGBBlack );
  261.                     PenSize( 1,1) ;
  262.                     InsetRect( &iRect, -4, -4 ) ;
  263.                     FrameRect( &iRect ) ;
  264.                     PenNormal() ;
  265.                 }
  266.                 
  267.                 break ;
  268.                 
  269.             case kColorSelectionDialogBGColorBox:
  270.                 // get the users choice
  271.                 if( GetColor( where, "\pEnter the Background color:", &theBGColor, &theBGColor) ) {
  272.                 
  273.                     // set up the background color
  274.                     GetDItem ( theColorSelectionDialog, kColorSelectionDialogBGColorBox, &iKind, &iHandle, &iRect) ;
  275.     
  276.                     RGBForeColor ( &theBGColor );
  277.                     FillRect( &iRect , &black) ;
  278.                     
  279.                     RGBForeColor ( &kRGBBlack );
  280.                     PenSize( 1,1) ;
  281.                     InsetRect( &iRect, -4, -4 ) ;
  282.                     FrameRect( &iRect ) ;
  283.                     PenNormal() ;
  284.                 } 
  285.                 
  286.                 break ;
  287.                 
  288.             default:
  289.                     break ;
  290.                     
  291.         }
  292.         
  293.     } while( itemHit != ok && itemHit != cancel) ;
  294.     DisposDialog ( theColorSelectionDialog );
  295.     
  296.     
  297.     SetPort( (GrafPtr)imageToColorize);
  298.     if( itemHit == ok )
  299.         draw_image(    (CGrafPtr)imageToColorize, 
  300.                     0, 
  301.                     0,
  302.                     &theFGColor, 
  303.                     &theBGColor) ;
  304.  
  305.     SetPort( savedPort ) ;
  306.     
  307.  
  308. }
  309.     
  310.     
  311.